package com.perseygroup.apps;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;

public class GeneralActivity extends Activity {
	
	//Result for activity when execute finish method
	protected final static int RESULT_DECONNECTER = 1;
	protected final static int RESULT_QUITTER = 2;
	//The request code for startActivityForResult
	protected final static int CREATE_REQUEST_CODE = 1;
	//The result code when trying connexion
	protected int resultSubmit = -100;//default value
	//The flag used to test how we get to authentification.activity
	protected static boolean FROM_MENU_QUITTER = false;
	
	//Prefs Objects
	protected static final String PREFS_STUDENT = "MyPerseyStudentPrefs";//Save student prefs
	protected static String PREFS_STUDENT_LOGIN = "PREFS_STUDENT_LOGIN";//Save student login
	protected static String PREFS_STUDENT_PASSW = "PREFS_STUDENT_PASSW";//Save student password
	protected static String PREFS_STUDENT_SCHOOL = "PREFS_STUDENT_SCHOOL";//Save student school
	
	protected SharedPreferences myPerseyStudentPrefs;
	
	//Set Login and School in prefs
	protected void setStudentDataInPrefs(String sLogin, String sSchool, String sPass)
	{
		this.myPerseyStudentPrefs = getSharedPreferences(PREFS_STUDENT, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyStudentPrefs.edit();
		prefsEditor.putString(PREFS_STUDENT_LOGIN, sLogin);//Set login
		prefsEditor.putString(PREFS_STUDENT_PASSW, sPass);//Set Pass
		prefsEditor.putString(PREFS_STUDENT_SCHOOL, sSchool);//Set school
		prefsEditor.commit();
	}
	
	//Get Login and School in prefs
	protected String[] getStudentDataFromPrefs()
	{
		this.myPerseyStudentPrefs = getSharedPreferences(PREFS_STUDENT, MODE_PRIVATE);
		String sLogin = this.myPerseyStudentPrefs.getString(PREFS_STUDENT_LOGIN, null);
		String sSchool = this.myPerseyStudentPrefs.getString(PREFS_STUDENT_SCHOOL, null);
		String sPass = this.myPerseyStudentPrefs.getString(PREFS_STUDENT_PASSW, null);
		String[] val = { sLogin, sSchool, sPass };//0-Login, 1-School, 2-Pass
		return val;
	}
	
	//Remove Login, Password and School in prefs
	protected void removeStudentDataFromPrefs()
	{
		this.myPerseyStudentPrefs = getSharedPreferences(PREFS_STUDENT, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyStudentPrefs.edit();
		prefsEditor.remove(PREFS_STUDENT_LOGIN);
		prefsEditor.remove(PREFS_STUDENT_PASSW);
		prefsEditor.remove(PREFS_STUDENT_SCHOOL);
		prefsEditor.commit();
	}
	
	//Remove the prefs checkbox
	//when we deconnect because the checkbox prefs
	//is just available for when we quit the application
	protected void removeCheckboxPrefs()
	{
		this.myPerseyStudentPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor prefsEditor = this.myPerseyStudentPrefs.edit();
		prefsEditor.putBoolean("pref_student_data_connexion", false);//Set school
		prefsEditor.commit();
	}
	
	/**
     * Converts an input stream to a string
     * 
     * @param is
     *            The {@code InputStream} object to read from
     * @return A {@code String} object representing the string for of the input
     * @throws IOException
     *             Thrown on read failure from the input
     */
    protected String inputStreamToString(InputStream is) throws IOException {
        StringBuffer sBuffer = new StringBuffer();
        DataInputStream dataIO = new DataInputStream(is);
        String strLine = null;
        while ((strLine = dataIO.readLine()) != null) {
            sBuffer.append(strLine + "\n");
        }
        dataIO.close();
        is.close();
        return sBuffer.toString();
    }
}
